package com.company.algo.listnode;

import com.company.algo.pojo.ListNode;

public class DeleteLastNthNode {
    public ListNode removeNthFromEnd (ListNode head, int n) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode p = dummy, q= dummy;
        for (int i = 0; i < n + 1; i++) {
            q = q.next;
        }
        while (q!=null){
            p = p.next;
            q = q.next;
        }
        ListNode deleted = p.next;
        p.next = deleted.next;
        return dummy.next;
    }
}
